package com.spring.sunflower.common

import android.content.Context
import android.graphics.Color
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.Glide
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseFragment
import com.spring.sunflower.bean.BalanceBean
import com.spring.sunflower.bean.TaskAwardDailyBean
import com.spring.sunflower.bean.TaskAwardSignBean
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.util.DisplayUtils
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 * Created by Kevin on 2021/7/17<br/>
 *
 * Describe:<br/>
 */
class AwardFragment : BaseFragment<AwardPresenter>(), CommonContract.AwardView {
    private var tvCoin: TextView? = null
    private var tvDone: TextView? = null
    private var rvRecyclerView: RecyclerView? = null

    //    private var mAdapter: BalanceAdapter? = null
    private var mData: MutableList<BalanceBean.Data> = ArrayList()
    private var clickPos = 0
    private var titleList = arrayListOf<String>("每日任务", "新手任务", "每日签到")
    private var mFragments: MutableList<Fragment> = ArrayList()
    private var mAdapter: RelationAdapter? = null
    private var magicIndicator: MagicIndicator? = null
    private var mViewPager: ViewPager? = null
    private var tvTip: TextView? = null
    private var ivCoin: ImageView? = null
    override fun setLayoutResId(): Int = R.layout.fragment_award

    override fun initView() {
        val gender = getField(Constant.KEY_GENDER, "1")
        ivCoin = mView.findViewById(R.id.ivCoin)
        tvTip = mView.findViewById(R.id.tvTip)
        tvCoin = mView.findViewById(R.id.tvCoin)
        tvCoin?.text = getField(Constant.KEY_AWARD, "0")
        rvRecyclerView = mView.findViewById(R.id.rvRecyclerView)
        tvDone = mView.findViewById(R.id.tvDone)
        magicIndicator = mView.findViewById(R.id.magicIndicator)
        mViewPager = mView.findViewById(R.id.viewpager)
        mFragments.add(TaskDailyFragment())
        mFragments.add(TaskNewerFragment())
        mFragments.add(TaskSignFragment())
        initIndicator()
        mAdapter = RelationAdapter(childFragmentManager, titleList, mFragments)
        mViewPager?.let {
            it.adapter = mAdapter
            it.setOffscreenPageLimit(2)
            it.setCurrentItem(0)
        }
        Glide.with(mActivity)
            .load(if ("1" == gender) R.drawable.ic_money else R.drawable.ic_income_diamond)
            .placeholder(R.drawable.ic_placeholder)
            .into(ivCoin!!)
    }

    override fun onResume() {
        super.onResume()
        tvCoin?.text = getField(Constant.KEY_AWARD, "0")
    }

    override fun initData() {
    }

    override fun initListener() {
        tvTip?.setOnClickListener {
            startNewActivity(TaskActivity::class.java)
        }
    }

    override fun loadData() {
    }

    override fun setPresenter(): AwardPresenter {
        return AwardPresenter(this)
    }

    private fun initIndicator() {
        val commonNavigator = CommonNavigator(mActivity)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return titleList.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView: SimplePagerTitleView =
                    ColorTransitionPagerTitleView(context)
                simplePagerTitleView.text = titleList[index]
                simplePagerTitleView.width = DisplayUtils.getScreenWidth(mActivity) / 4
                simplePagerTitleView.normalColor = Color.parseColor("#999999")
                simplePagerTitleView.selectedColor = Color.parseColor("#000000")
                simplePagerTitleView.setOnClickListener { mViewPager!!.currentItem = index }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator? {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.lineHeight = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 20.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                indicator.yOffset = UIUtil.dip2px(context, 5.0).toFloat()
                indicator.setColors(Color.parseColor("#FADB4A"))
                return indicator
            }
        }
        magicIndicator!!.navigator = commonNavigator
        ViewPagerHelper.bind(magicIndicator, mViewPager)
    }

    override fun showDailyAward(bean: MutableList<TaskAwardDailyBean.DatasBean>?) {
    }

    override fun showNewerAward(bean: MutableList<TaskAwardDailyBean.DatasBean>?) {
    }

    override fun showSignAward(bean: MutableList<TaskAwardSignBean.DatasBean>?) {
    }


}